<?php

namespace App\Listener;

use App\Event\UserChatEvent;
use App\Helpers\RedisUtil;
use App\Model\Gift;
use App\Model\GiftLog;
use App\Model\Message;
use App\Model\Service\GiftService;
use App\Model\UserChat;
use App\Model\UserProfile;
use Exception;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 接收系统礼物 - 监听器
 * @Listener 
 */
class GetSysGiftListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            //发送IM消息事件
            UserChatEvent::class,
        ];
    }

    /**
     * @param object $event
     * @return array|false|void
     * @throws Exception
     */
    public function process(object $event)
    {
        $userId = $event->userId;
        $chatUserId = $event->chatUserId;

        //非声优不处理
        $profile = UserProfile::getProfile($userId);
        if (!$profile) return false;
        if ($profile->gender == 1) {
            return $this->proccessUserGift($userId, $chatUserId, UserProfile::ROLE_USER);
        }


        if ($profile->gender == 0) {
            $this->proccessUserGiftFemalePush($userId, $chatUserId, $profile->gender);
        }

        if (UserProfile::checkRoleHost($profile->role)) {
//            $this->proccessUserGiftFemalePush($userId, $chatUserId, $profile->gender);
            return $this->proccessUserGift($userId, $chatUserId, UserProfile::ROLE_HOST);
        }

    }

    /**
     * @param $userId
     * @param $chatUserId
     * @param $gender
     * @return array|false
     * @throws Exception
     */
    function proccessUserGiftFemalePush($userId, $chatUserId, $gender)
    {
        $key = "GetSysPushGiftListener_{$userId}_{$chatUserId}";
        if (RedisUtil::concurrentLock($key, 3)) {
            log_info(__CLASS__ . "==fetchpushgift_proccessing==Key:{$key}");
            return false;
        }

        $expNum = GiftLog::expireRecordsPush($chatUserId, $userId);
        if ($expNum > 0) {
            Message::sendImSystemTip($userId, $chatUserId, '部分礼物已经过期，无法领取～', 2);
            //发送礼物失效透传消息
            Message::sendSlient($userId, 'AUTO_EXPIRE_GIFT');
        }

        //领取所有礼物
        $result = GiftService::fetchAllPushGift($chatUserId, $userId);
        if (!$result) return false;

        //领取礼物数量
        // $giftNum = $result['gift_num'] ?? 1;
        // $giftId = $result['gift_id'] ?? 1;
        //发送消息
        $tip = $gender == 0 ? '已收下礼物，可在“我的账户”查看明细或提现~' : '已收下礼物，可在“我的账户”查看明细';
        Message::sendImSystemTip($userId, $chatUserId, $tip, 2);

        //发送透传消息
        Message::sendSlient($userId, 'AUTO_RECEIVE_GIFT');
        return $result;
    }

    /**
     * 主播领取领取礼物
     */
    function proccessUserGift($userId, $chatUserId, $role = 2)
    {
        //加入同步锁
        $key = "GetSysGiftListener_{$userId}_{$chatUserId}";
        if (RedisUtil::concurrentLock($key, 3)) {
            log_info(__CLASS__ . "==fetchgift_proccessing==Key:{$key}");
            return false;
        }

        //失效的记录状态修改
        $hostSendMale = $role == UserProfile::ROLE_HOST ? false : true;
        $expNum = GiftLog::expireRecords($chatUserId, $userId, $hostSendMale);
        if ($expNum > 0) {
            Message::sendImSystemTip($userId, $chatUserId, '部分礼物已经过期，无法领取～', 2);
            //发送礼物失效透传消息
            Message::sendSlient($userId, 'AUTO_EXPIRE_GIFT');
        }

        //领取所有礼物
        $result = GiftService::fetchAll($chatUserId, $userId);
        if (!$result) return false;

        //领取礼物数量
        // $giftNum = $result['gift_num'] ?? 1;
        // $giftId = $result['gift_id'] ?? 1;
        //发送消息
        $tip = $role == UserProfile::ROLE_HOST ? '已收下礼物，可在“我的账户”查看明细或提现~' : '已收下礼物，可在“我的账户”查看明细';
        Message::sendImSystemTip($userId, $chatUserId, $tip, 2);

        //发送透传消息
        Message::sendSlient($userId, 'AUTO_RECEIVE_GIFT');
        return $result;
    }
}
